home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / Talking Clock Pro™ 2.0b2 / Talking Clock Pro Source / Controller / Source / apprec.c next >
Encoding:
C/C++ Source or Header  |  1993-09-04  |  7.7 KB  |  394 lines  |  [TEXT/KAHL]

  1. /*
  2.  * apprec.c
  3.  */
  4.  
  5. #include "apprec.h"
  6. #include "menu.h"
  7. #include "x.h"
  8. #include "clockrec.h"
  9. #include "clwinrec.h"
  10.  
  11.  
  12. DefWindowRec appRec = {
  13.  
  14.     128 , NULL ,
  15.  
  16.     AppCr ,
  17.     AppDe ,
  18.     AppUp ,
  19.     AppMD ,
  20.     AppMU ,
  21.     AppKD ,
  22.     AppAK ,
  23.     AppAc ,
  24.     AppSw ,
  25.     AppId ,
  26.     AppPr ,
  27.     AppCo ,
  28.     AppAE
  29.  
  30. } ;
  31.  
  32.  
  33. long startedTicks = 60000 ;
  34.  
  35. #define CURRENT_VER 3
  36.  
  37. typedef struct AppData {
  38.  
  39.     FSSpec            theFile ;
  40.     Handle            theWindows ;
  41.  
  42. } AppData , * AppPtr ;
  43.  
  44.  
  45. static void
  46. AppAboutItem ( ) {
  47.     Alert ( 129 , NULL ) ;
  48. }
  49.  
  50.  
  51. static void
  52. AppAppleMenu ( short item , unsigned char * itemStr ) {
  53.  
  54.     if ( item == ABOUT_ITEM ) {
  55.         AppAboutItem ( ) ;
  56.     } else {
  57.         OpenDeskAcc ( itemStr ) ;
  58.     }
  59. }
  60.  
  61.  
  62. void
  63. ClWinAdd ( WindowPtr wp , Handle data ) {
  64.  
  65. AppPtr ptr = ( AppPtr ) * data ;
  66.  
  67.     PtrAndHand ( ( Ptr ) & wp , ptr -> theWindows , sizeof ( WindowPtr ) ) ;
  68. }
  69.  
  70.  
  71. void
  72. ClWinRemove ( WindowPtr wp , Handle data ) {
  73.  
  74. AppPtr ptr = ( AppPtr ) * data ;
  75. WindowPtr * wpp = ( WindowPtr * ) * ( ptr -> theWindows ) ;
  76. int num = GetHandleSize ( ptr -> theWindows ) / sizeof ( WindowPtr ) ;
  77. Boolean del = 0 ;
  78.  
  79.     while ( num -- ) {
  80.         if ( del ) {
  81.             wpp [ -1 ] = * wpp ;
  82.         } else {
  83.             if ( * wpp == wp ) {
  84.                 del = 1 ;
  85.             }
  86.         }
  87.         wpp ++ ;
  88.     }
  89.     SetHandleSize ( ptr -> theWindows , GetHandleSize ( ptr -> theWindows ) -
  90.         sizeof ( WindowPtr ) ) ;
  91. }
  92.  
  93.  
  94. static void
  95. AddPrefResource ( AppPtr ptr ) {
  96.  
  97. volatile Handle h = NULL ;
  98. volatile short refNum ;
  99.  
  100.     TRY {
  101.         h = GetResource ( 'STR ' , -16397 ) ; /* Magic prefs string */
  102.         FailNil ( h ) ;
  103.         DetachResource ( h ) ;
  104.         FSpCreateResFile ( & ( ptr -> theFile ) , 'pref' , 'pref' , smSystemScript ) ;
  105.         refNum = FSpOpenResFile ( & ( ptr -> theFile ) , fsRdWrPerm ) ;
  106.         if ( refNum < 1 ) {
  107.             FailErr ( ResError ( ) ) ;
  108.         }
  109.         AddResource ( h , 'STR ' , -16397 , "\p" ) ;
  110.         h = NULL ;
  111.         FailErr ( ResError ( ) ) ;
  112.     } CLEANUP {
  113.         if ( refNum > 0 ) {
  114.             CloseResFile ( refNum ) ;
  115.             refNum = 0 ;
  116.         }
  117.         if ( h ) {
  118.             DisposeHandle ( h ) ;
  119.         }
  120.     } DONE ;
  121. }
  122.  
  123.  
  124. void
  125. AppSaveWindows ( Handle data ) {
  126.  
  127. AppPtr ptr = ( AppPtr ) * data ;
  128. WindowPtr * wpp = ( WindowPtr * ) * ( ptr -> theWindows ) ;
  129. short num = GetHandleSize ( ptr -> theWindows ) / sizeof ( WindowPtr ) ;
  130. short refNum = 0 , temp , ver ;
  131. long size ;
  132.  
  133.     TRY {
  134.         HLock ( ptr -> theWindows ) ;
  135.         FSpCreate ( & ( ptr -> theFile ) , 'pref' , 'pref' , smSystemScript ) ;
  136.         FailErr ( FSpOpenDF ( & ( ptr -> theFile ) , fsRdWrPerm , & refNum ) ) ;
  137.         FailErr ( SetEOF ( refNum , 0L ) ) ;
  138.         ver = CURRENT_VER ;
  139.         size = sizeof ( ver ) ;
  140.         FailErr ( FSWrite ( refNum , & size , ( Ptr ) & ver ) ) ;
  141.         size = sizeof ( num ) ;
  142.         FailErr ( FSWrite ( refNum , & size , ( Ptr ) & num ) ) ;
  143.         while ( num -- ) {
  144.             ClWinWrite ( refNum , * wpp ) ;
  145.             wpp ++ ;
  146.         }
  147.     } CLEANUP {
  148.         HUnlock ( ptr -> theWindows ) ;
  149.         if ( refNum ) {
  150.             FSClose ( refNum ) ;
  151.             refNum = 0 ;
  152.         }
  153.     } DONE ;
  154.     AddPrefResource ( ptr ) ;
  155. }
  156.  
  157.  
  158. static void
  159. AppFileMenu ( short item , Handle data ) {
  160.  
  161. WindowPtr wp ;
  162. extern Boolean hasController ;
  163.  
  164.     switch ( item ) {
  165.     case NEW_ITEM :
  166.         wp = MakeWindow ( NULL , & clwinRec ) ;
  167.         if ( wp ) {
  168.             SelectWindow ( wp ) ;
  169.             ShowWindow ( wp ) ;
  170.         }
  171.         break ;
  172.     case OPEN_ITEM :
  173.         if ( ! hasController ) {
  174.             wp = MakeWindow ( NULL , & clockRec ) ;
  175.             SelectWindow ( wp ) ;
  176.             ShowWindow ( wp ) ;
  177.         }
  178.         break ;
  179.     case QUIT_ITEM :
  180.         quit = 1 ;
  181.         break ;
  182.     }
  183. }
  184.  
  185.  
  186. static void
  187. ReadWindows ( FSSpec * fss ) {
  188.  
  189. volatile short refNum = 0 ;
  190. long size ;
  191. short num , ver ;
  192. WindowPtr wp ;
  193. volatile Boolean del = 0 ;
  194.  
  195.     TRY {
  196.         if ( ! FSpOpenDF ( fss , fsRdPerm , ( short * ) & refNum ) ) {
  197.             size = sizeof ( ver ) ;
  198.             FailErr ( FSRead ( refNum , & size , ( Ptr ) & ver ) ) ;
  199.             if ( ver != CURRENT_VER ) {
  200.                 Alert ( 132 , NULL ) ;
  201.                 del = 1 ;
  202.             }
  203.             if ( ! del ) {
  204.                 size = sizeof ( num ) ;
  205.                 FailErr ( FSRead ( refNum , & size , ( Ptr ) & num ) ) ;
  206.                 while ( num -- ) {
  207.                     wp = MakeWindow ( NULL , & clwinRec ) ;
  208.                     ClWinRead ( refNum , wp ) ;
  209.                     if ( wp ) {
  210.                         SelectWindow ( wp ) ;
  211.                         ShowWindow ( wp ) ;
  212.                     }
  213.                 }
  214.             }
  215.         }
  216.     } CLEANUP {
  217.         if ( refNum ) {
  218.             FSClose ( refNum ) ;
  219.             refNum = 0 ;
  220.         }
  221.     } DONE ;
  222.     if ( del ) {
  223.         FailErr ( FSpDelete ( fss ) ) ;
  224.     }
  225. }
  226.  
  227.  
  228. /*    This function will be called with a preferences file spec    */
  229. /*    The name of the prefs file is specified in STR id 1            */
  230. OSErr
  231. AppCr ( WindowPtr wp , Handle * data , FSSpec * file ) {
  232.  
  233.     startedTicks = TickCount ( ) + 180L ;
  234.     * data = NewHandle ( sizeof ( AppData ) ) ;
  235.     FailNil ( * data ) ;
  236.     HLockHi ( * data ) ;
  237.     ( ( AppPtr ) * * data ) -> theFile = * file ;
  238.     ( ( AppPtr ) * * data ) -> theWindows = NewHandle ( 0L ) ;
  239.  
  240.     ReadWindows ( file ) ;
  241.  
  242.     return noErr ;
  243. }
  244.  
  245.  
  246. /*    This is a good place to write back your preferences; it is    */
  247. /*    called right before the application quits                    */
  248. OSErr
  249. AppDe ( WindowPtr wp , Handle data ) {
  250.  
  251.     return noErr ;
  252. }
  253.  
  254.  
  255. /*    Update events for windows that don't handle them themselves    */
  256. /*    are sent here.                                                */
  257. OSErr
  258. AppUp ( WindowPtr wp , Handle data , EventRecord * event ) {
  259.     return noErr ;
  260. }
  261.  
  262.  
  263. /*    MouseDown events in window content that are not handled by    */
  264. /*    windows are sent here                                        */
  265. OSErr
  266. AppMD ( WindowPtr wp , Handle data , EventRecord * event ) {
  267.     return noErr ;
  268. }
  269.  
  270.  
  271. /*    MouseUp events in window content that are not handled by    */
  272. /*    windows are sent here                                        */
  273. OSErr
  274. AppMU ( WindowPtr wp , Handle data , EventRecord * event ) {
  275.     return noErr ;
  276. }
  277.  
  278.  
  279. /*    KeyDown events in window content that are not handled by    */
  280. /*    windows are sent here                                        */
  281. OSErr
  282. AppKD ( WindowPtr wp , Handle data , EventRecord * event ) {
  283.     return noErr ;
  284. }
  285.  
  286.  
  287. /*    AutoKey events in window content that are not handled by    */
  288. /*    windows are sent here                                        */
  289. OSErr
  290. AppAK ( WindowPtr wp , Handle data , EventRecord * event ) {
  291.     return noErr ;
  292. }
  293.  
  294.  
  295. /*    Activate events for windows that are not handled by the        */
  296. /*    window are sent here                                        */
  297. OSErr
  298. AppAc ( WindowPtr wp , Handle data , EventRecord * event ) {
  299.     return noErr ;
  300. }
  301.  
  302.  
  303. /*    MultiFinder events for windows that are not handled by the    */
  304. /*    window are sent here                                        */
  305. OSErr
  306. AppSw ( WindowPtr wp , Handle data , EventRecord * event ) {
  307.  
  308. extern Boolean hasController ;
  309.  
  310.     if ( ( ! FrontWindow ( ) ) || ( TickCount ( ) > startedTicks ) ) {
  311.         if ( ( event -> message & 1 ) && ! hasController ) {
  312.             wp = MakeWindow ( NULL , & clockRec ) ;
  313.             SelectWindow ( wp ) ;
  314.             ShowWindow ( wp ) ;
  315.         }
  316.     }
  317.     return noErr ;
  318. }
  319.  
  320.  
  321. /*    When the front window does not handle idling, this is        */
  322. /*    called                                                        */
  323. OSErr
  324. AppId ( WindowPtr wp , Handle data , long * sleep ) {
  325.  
  326. AppPtr ptr = ( AppPtr ) * data ;
  327. WindowPtr * wpp = ( WindowPtr * ) * ( ptr -> theWindows ) ;
  328. int num = GetHandleSize ( ptr -> theWindows ) / sizeof ( WindowPtr ) ;
  329. DateTimeRec dtr ;
  330. unsigned long now ;
  331.  
  332.     GetDateTime ( & now ) ;
  333.     Secs2Date ( now , & dtr ) ;
  334.     HLock ( ptr -> theWindows ) ;
  335.     TRY {
  336.         while ( num -- ) {
  337.             if ( * wpp ) {
  338.                 ClWinRunClock ( * wpp , sleep , dtr . minute , dtr . second ) ;
  339.             }
  340.             wpp ++ ;
  341.         }
  342.     } CLEANUP {
  343.         HUnlock ( ptr -> theWindows ) ;
  344.     } DONE ;
  345.  
  346.     return noErr ;
  347. }
  348.  
  349.  
  350. /*    This is called when menus need to be updated.                */
  351. OSErr
  352. AppPr ( WindowPtr wp , Handle data ) {
  353.  
  354. extern Boolean hasController ;
  355.  
  356.     EnableCmd ( APPLE_MENU , ABOUT_ITEM ) ;
  357.  
  358.     EnableCmd ( FILE_MENU , NEW_ITEM ) ;
  359.     if ( ! hasController ) {
  360.         EnableCmd ( FILE_MENU , OPEN_ITEM ) ;
  361.     }
  362.     EnableCmd ( FILE_MENU , QUIT_ITEM ) ;
  363.  
  364.     return noErr ;
  365. }
  366.  
  367.  
  368. /*    This is called when the front window does not handle        */
  369. /*    commands                                                    */
  370. OSErr
  371. AppCo ( WindowPtr wp , Handle data , short menu , short item ,
  372.     unsigned char * itemStr ) {
  373.     switch ( menu ) {
  374.     case APPLE_MENU :
  375.         AppAppleMenu ( item , itemStr ) ;
  376.         break ;
  377.     case FILE_MENU :
  378.         AppFileMenu ( item , data ) ;
  379.         break ;
  380.     }
  381.     return noErr ;
  382. }
  383.  
  384.  
  385. /*    This is called when the front window does not handle        */
  386. /*    AppleEvents                                                    */
  387. OSErr
  388. AppAE ( WindowPtr wp , Handle data , AppleEvent * event ,
  389.     AppleEvent * reply ) {
  390.     return noErr ;
  391. }
  392.  
  393.  
  394.